package thinking.in.spring.ioc.overview.container;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import thinking.in.spring.ioc.overview.domain.User;

import java.util.Map;

/**
 * BeanFactory Ioc 容器示例
 *
 * @author Ringo
 * @date 2021/8/22 21:00
 */
public class BeanFactoryAsIocContainerDemo {

    public static void main(String[] args) {
        // 1: 创建 BeanFactory 容器
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        // 2: 加载配置
        // 构造器参数是 BeanDefinitionRegistry
        // DefaultListableBeanFactory 恰好实现了BeanDefinitionRegistry
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        String location = "classpath:META-INF/dependency-look-context.xml";
        // 返回加载 Bean Definitions 的数量
        int count = reader.loadBeanDefinitions(location);
        System.out.println("加载 Bean Definition 的数量: " + count);

        // 3: 依赖查找
        Map<String, User> users = beanFactory.getBeansOfType(User.class);
        System.out.println("查找到容器中所有 User Bean: " + users);
    }
}
